#define SSD_128_64
#define MENU_HEIGHT 20 //菜单行高


#include <Adafruit_SSD1306.h>


#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

typedef void (*CallBackFunction) (byte dataType,byte data[],byte len);

class OLEDDisplay{
  public:
    byte F_Index =255;//界面索引
    Adafruit_SSD1306 oled;
    float Voltage=0.00;//电池电压
    byte Mode=255;//工作模式
    unsigned int Freq=1;//数据频率
    byte RadioType=255;//协议类型
    byte isDC=255;
    byte AntIndex=255;//天线位置索引

   
    bool isRef=false;



    void Init(){
      oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); 
      
      
      oled.clearDisplay(); //清屏
      oled.setTextColor(WHITE);
      oled.setTextSize(1);             //设置字体大小  
      oled.setCursor(20,10);             //设置字体的起始位置
      oled.print("Antenna Switch");   //输出字符并换行
      
      oled.setCursor(38,30);             //设置字体的起始位置
      oled.print("By:BG1JT");   //输出字符并换行
      oled.setCursor(44,46);
      oled.print("V:0.1");   //输出字符并换行
      oled.display();    
    }

    void ShowFrm(byte frmId)
    {
      F_Index=frmId;         
      switch(frmId)
      {
        case 1:
          MainShow();
        break;
        case 2:
          ShowMenu(0);
        break;
      }
    }
    void ref_display(){
      if(DisplayRef)
      {
        oled.display();
        DisplayRef=false;
        //Serial.println("display");
      }
      
    }
   
    /*主界面*/
   
    void MainShow(){      
   
      F_Index=1;
      oled.clearDisplay(); //清屏     
      
      //setTextColor(BLACK, WHITE); 
      oled.setTextColor(WHITE); 
      oled.setTextSize(1);
      
      oled.setCursor(60,45);
      oled.print("Mode:");
      
      oled.setCursor(0,57);
      oled.print("F:");

      oled.setCursor(60,57);
      oled.print("R:");
      oled.drawLine(0, 41, 128, 41, WHITE);

      
      oled.fillCircle(18, 10, 10, WHITE);
      oled.fillCircle(18, 10, 6, BLACK);
      oled.fillCircle(18, 10, 2, WHITE);
      
      oled.fillCircle(64, 10, 10, WHITE);
      oled.fillCircle(64, 10, 6, BLACK);
      oled.fillCircle(64, 10, 2, WHITE);

      oled.fillCircle(109, 10, 10, WHITE);
      oled.fillCircle(109, 10, 6, BLACK);
      oled.fillCircle(109, 10, 2, WHITE);

      
      oled.setTextColor(WHITE);
      oled.setTextSize(1);     
      oled.setCursor(3,28);
      oled.print("ANT-A");   

      oled.setCursor(57,28);
      oled.print("COM");   
      
      oled.setCursor(94,28);
      oled.print("ANT-B");   
      
      isRef=true;   
      setFreq(0);
      
    
      
      //setAntIndex(0);
      SetLock(false);
      isRef=false;
      DisplayRef=true;
      
    }

    void SetDefMainVal(){
      isRef=true;
      setMode(Mode);  
      SetRadioType(RadioType);
      setAntIndex(AntIndex); 
      setPowerInfo(isDC);
      isRef=false;
      DisplayRef=true;
    }

 

    void setPowerInfo(byte val){
  
      if(isDC==val && !isRef)return;
      
      
      oled.fillRect(0, 45, 60, 8, BLACK);
      oled.setTextColor(WHITE);
      oled.setTextSize(1);
      oled.setCursor(0,45);
      if(val)
      {        
         oled.print("DC IN");
      }
      else
      {       
         oled.print("Bat:");
         oled.setCursor(50,45);
         oled.print("v");
      }
      isDC=val;
      DisplayRef=true;
    }

    void setVoltage(float val)
    {
      
      if(val==Voltage || isDC)return;
      
      oled.fillRect(24, 45, 25, 8, BLACK);
      oled.setTextColor(WHITE);
      oled.setTextSize(1);
      oled.setCursor(24,45);
      
      oled.print(val);
      
      Voltage=val;
      DisplayRef=true;
    }

    void setMode(byte val){
      if(Mode==val && !isRef)return;
 
      oled.fillRect(90, 45, 35, 8, BLACK);
      oled.setTextColor(WHITE);
      oled.setTextSize(1);
      oled.setCursor(90,45);
      if(val==0){
        oled.print("Auto");
      }
      else{
        oled.print("Manual");
      }
      Mode=val;
      DisplayRef=true;
    }

    void setFreq(unsigned int val)
    {      
      if(Freq==val && !isRef)return;
      
      oled.fillRect(13, 57, 43, 8, BLACK);
      oled.setTextColor(WHITE);
      oled.setTextSize(1);
      oled.setCursor(13,57);
      
      if(val==0)
      {oled.print("None");}
      else{
        oled.print(val);
        oled.print("Mhz");        
      }
      Freq=val;
      DisplayRef=true;
    }

    void SetRadioType(byte val)
    {
      if(RadioType==val && !isRef)return;
      oled.setTextColor(WHITE);
      oled.setTextSize(1);
      oled.fillRect(72, 57, 53, 8, BLACK);
      oled.setCursor(72,57);
      switch(val)
      {
        case 1:
          oled.print("FT817");
        break;
        case 2:
          oled.print("Q900");
        break;
        default:
          oled.print("None");
        break;
      }
      RadioType=val;
      DisplayRef=true;
    }

    void setAntIndex(byte val)
    {
      if(AntIndex==val && !isRef )return;
      uint16_t A1,A2;
    
      if(val==0)
      {        
        A1 =WHITE;
        A2=BLACK;
      }
      else{        
        A1 =BLACK;
        A2=WHITE;
      }
      oled.drawLine(33, 10, 40, 5, A1);      
      oled.fillRect(33, 10, 15, 2,A1);
      oled.drawLine(33, 11, 40, 16, A1);
      
      oled.drawLine(94, 10, 87, 5, A2);        
      oled.fillRect(79, 10, 15, 2,A2);
      oled.drawLine(94, 11, 87, 16, A2);
      AntIndex=val;
      DisplayRef=true;
     
    }

    void SetLock(bool isLock){
      
      if(isLock){
        oled.fillRect(0, 0, 8, 8, WHITE);
        oled.setTextColor(BLACK);
        oled.setTextSize(1);      
        oled.setCursor(1,0);
        oled.print("L");
      }   
      else{oled.fillRect(0, 0, 8, 8, BLACK);}
      DisplayRef=true;
    }
    /*主界面 END*/
    /*设置界面*/
   
    
    //显示二级菜单
    void ShowMenu(byte m_index,byte chengMenu)
    {
      F_Index=2;
      MenuIndex=m_index;  
      ChengMenu=chengMenu;
      oled.clearDisplay(); //清屏
          
      switch(m_index)
      {
        case 1:       
           _displayMenu(Menus_1,chengMenu,2);        
        break;
        case 2:
          _displayMenu(Menus_2,chengMenu,3);
        break;
      }
     
      //oled.display();
      DisplayRef=true;
    }
    
    void ShowMenu(byte chengMenu){
      F_Index=2;
      oled.clearDisplay(); //清屏
      ChengMenu=chengMenu;
      MenuIndex=0;

     _displayMenu(Menus_Main,chengMenu,3);
      
      //oled.display();
      DisplayRef=true;
    }
        
    void SelectMenu(bool isUp){
      Serial.println(isUp);
      
      if(MenuIndex==0)
      {    
        if(isUp)
        {ChengMenu++;}
        else{ChengMenu--;}
        if(ChengMenu>MENU_MAIN_COUNT-1)ChengMenu=MENU_MAIN_COUNT-1;
        ShowMenu(ChengMenu);    
      }
      else{
        if(isUp)
        {ChengMenu++;}
        else{ChengMenu--;}
        
        switch(MenuIndex)
        {
          case 1:       
             if(ChengMenu>MENU_1_COUNT-1)ChengMenu=MENU_1_COUNT-1;            
          break;
          case 2:
            if(ChengMenu>MENU_2_COUNT-1)ChengMenu=MENU_2_COUNT-1;             
          break;
        }
        ShowMenu(MenuIndex,ChengMenu);
      }
    }

    void MenuClick(CallBackFunction callback){
      if(MenuIndex==0)
      {
        //主菜单
        switch(ChengMenu)
        {
          case 0:
            //模式
            ShowMenu(1,0);
          break; 
          case 1:
            //协议           
            ShowMenu(2,0);
          break;
          case 2:
            //退出
            MenuIndex=0; 
            ChengMenu=0;  
            MainShow();
            SetDefMainVal();
          break;
        }        
        callback(0,{0},0);
    
      }
      else
      {
        byte data[2]={0x00,ChengMenu};
        if(MenuIndex==1){
          //模式设置
          data[0]=0x04;        
        }
        else if(MenuIndex==2){
          //协议设置
           data[0]=0x03;        
          
        }               
        ShowMenu(0); 
        callback(1,data,3);
        
      }
    }
    /*设置界面 END*/
    
     
private:
/*设置界面*/
  byte MenuIndex=0;
  byte ChengMenu=0;
  bool DisplayRef=false;
  static const byte MENU_MAIN_COUNT =3;//主菜单数
  static const byte MENU_1_COUNT =2;//菜单1
  static const byte MENU_2_COUNT =3;//菜单2

  const String Menus_Main[MENU_MAIN_COUNT] PROGMEM ={"Model","Radio","Exit"};
  const String Menus_1[MENU_1_COUNT] PROGMEM ={"Auto","Manual"};
  const String Menus_2[MENU_2_COUNT] PROGMEM ={"None","FT817","Q900"};
  
  
  void _displayMenu(String menus[],byte menuIndex,byte len){
      oled.setTextSize(2);      
      for(byte i=0;i<len;i++)
      {
        oled.setCursor(2,i*MENU_HEIGHT+1);
        if(i==menuIndex)
        {        
           oled.fillRect(0, i*MENU_HEIGHT-2, 128, MENU_HEIGHT, WHITE);
           oled.setTextColor(BLACK);
        }
        else
        {
          oled.fillRect(0, i*MENU_HEIGHT-2, 128, MENU_HEIGHT, BLACK);
          oled.setTextColor(WHITE);
        }
        oled.print(menus[i]);
      }
  }
  /*设置界面 END*/
};
